'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:SignChooselocationCtrl
 * @description
 * # SignChooselocationCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
  .controller('SignChooselocationCtrl', function ($scope, Util, localStorageService, MapService) {
    // 默认地址坐标
    var vm = this;
    var param = localStorageService.get("signparam");
    // 默认地址坐标
    vm.point = {
      longitude: 115.863834,
      latitude: 28.68003
    };
    this.ak = MapService.getAk();

    this.locit = function (pos, index) {
      vm.posindex = index;
      vm.positon = pos;
      MapService.setMarker(vm.map, vm.mapOptions, vm.positon.point)
    }
    // 设置考勤位置
    this.setCheckPos = function () {
      console.log(vm.positon);
      param.point = vm.positon.point;
      param.address = vm.positon.address;
      param.title = vm.positon.title;
      localStorageService.set("signparam", param);
      Util.goBack();
    };
    // 初始化成功后将map和标记返回以便获取具体位置
    this.finishmap = function (map, previousMarkers) {
      vm.map = map
      vm.previousMarkers = previousMarkers;
      MapService.getcurPositon().then(function (data) {
        vm.point.lat = data.latitude;
        vm.point.lng = data.longitude;
        vm.mapOptions.markers=[vm.point]
        displayPOI()
      }, function (data) {
        Util.Toast(data);
      });
    };
    var displayPOI = function () {
      var mOption = {
        poiRadius: 500,           //半径为500米内的POI,默认100米
        numPois: 20                //列举出50个POI,默认20个
      }
      var pointB = new BMap.Point(vm.point.lng, vm.point.lat);
      // var circle = new BMap.Circle(pointB, 500, { fillColor: "blue", strokeWeight: 1, fillOpacity: 0.3, strokeOpacity: 0.3 });
      // vm.map.addOverlay(circle);
      var myGeo = new BMap.Geocoder();        //创建地址解析实例
      myGeo.getLocation(pointB,
        function mCallback(rs) {
          vm.allPois = rs.surroundingPois;       //获取全部POI（该点半径为100米内有6个POI点）
          $scope.$apply()
        }, mOption
      );
    }
    // 初始化地图
    var initMap = function () {
      vm.offlineOpts = { retryInterval: 5000 };
      vm.mapOptions = {
        center: vm.point,
        zoom: 16,
        city: "NanChang",
        enableDragging: false
      };
    }
    initMap();
  });
